import cv2
import numpy as np
import matplotlib.pyplot as plt
import time

img0=cv2.imread(r'C:\Users\Hoshino Shen\Desktop\kong11.tif')      #原图待修复
scr=cv2.imread(r'C:\Users\Hoshino Shen\Desktop\chong1.tif')     #目标图填充图像
scr1=cv2.imread(r'C:\Users\Hoshino Shen\Desktop\kong1.tif')


#把两张图片转成灰度图片
img0=cv2.cvtColor(img0,cv2.COLOR_BGR2GRAY)
scr=cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)
scr1=cv2.cvtColor(scr1,cv2.COLOR_BGR2GRAY)
img=img0.copy()                                                 #做对比图

#映射列表
mHist1=[]           #各个灰度值像素个数
mNum1=[]            #累计灰度直方图（频率）
inhist1=[]
mHist2=[]
mNum2=[]
inhist2=[]

# 对修复图像进行均衡化

for i in range(256):
    mHist1.append(0)
row,col=scr1.shape               #获取修复图像像素点的宽度和高度

for i in range(row):
    for j in range(col):
        mHist1[scr1[i,j]]=mHist1[scr1[i,j]]+1    #统计各灰度值的个数
mNum1.append(mHist1[0]/scr1.size)


for i in range(0, 255):
    mNum1.append(mNum1[i]+mHist1[i+1]/scr1.size)
print(mNum1[0])

for i in range(256):
    inhist1.append(round(255*mNum1[i]))

# 对填充图像进行均衡化
for i in range(256):
    mHist2.append(0)
rows,cols=scr.shape               #获取填充图像像素点的宽度和高度

for i in range(rows):
    for j in range(cols):
        mHist2[scr[i,j]]=mHist2[scr[i,j]]+1     #统计灰度值的个数

mNum2.append(mHist2[0]/scr.size)

for i in range(0,255):
    mNum2.append(mNum2[i]+mHist2[i+1]/scr.size)

for i in range(256):
    inhist2.append(round(255*mNum2[i]))


#直方图匹配
g=[]                               #匹配后的图片像素
for i in range(256):
    a=mNum2[i]
    flag=True
    for j in range(256):
        if mNum1[j]==a:
            g.append(j)
            flag=False
            break
    if flag==True:
        minp=255
        for j in range(256):        #循环求最小差值
            b=abs(mNum1[j]-a)
            if b<minp:
                minp=b
                jmin=j
        g.append(jmin)

for i in range(row):        #读成图像
    for j in range(col):
        scr[i,j]=g[scr[i,j]]

cv2.imshow("before", scr)
cv2.waitKey(0)


#直方图匹配均值算法
'''avg1=0
for i in range(row):
    for j in range(col):
        avg1+=img0[i,j]/img0.size     #得出待修复图像匹配后灰度均值
print(avg1)

avg2=0
for i in range(row):
    for j in range(col):
        avg2+=scr[i,j]/scr.size     #得出填充图像匹配后灰度均值
print(avg2)

for i in range(row):
    for j in range(col):
        if img[i,j]<=1:
            img[i,j]=scr[i,j]+(avg1-avg2)

cv2.imwrite('E:\pingce\hdpj.tif',img)

cv2.imshow("img",img)
cv2.imshow("before",img0)
cv2.waitKey(0)'''
'''plt.hist(img.ravel(),256)
plt.show()'''